package com.code.zjm.firstapp;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity implements View.OnClickListener, ServiceConnection {
    private Button startService, stopService, bindService, unbindService, currentNumber;
    private Intent serviceIntent;
    private MainService mainService = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        System.out.println("onCreate");

        serviceIntent = new Intent(this, MainService.class);
        startService = (Button) findViewById(R.id.start_service);
        stopService = (Button) findViewById(R.id.stop_service);
        bindService = (Button) findViewById(R.id.bind_service);
        unbindService = (Button) findViewById(R.id.unbind_service);
        currentNumber = (Button) findViewById(R.id.current_number);
        startService.setOnClickListener(this);
        stopService.setOnClickListener(this);
        bindService.setOnClickListener(this);
        unbindService.setOnClickListener(this);
        currentNumber.setOnClickListener(this);
    }

    @Override
    protected void onStart() {
        super.onStart();
        System.out.println("onStart");
    }

    @Override
    protected void onResume() {
        super.onResume();
        System.out.println("onResume");
    }

    @Override
    protected void onPause() {
        super.onPause();
        System.out.println("onPause");
    }

    @Override
    protected void onStop() {
        super.onStop();
        System.out.println("onStop");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        System.out.println("onRestart");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        System.out.println("onDestroy");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.start_service:
                //启动服务，绑定服务需要先启动服务
                startService(serviceIntent);
                break;
            case R.id.stop_service:
                stopService(serviceIntent);
                break;
            case R.id.bind_service:
                bindService(serviceIntent, this, Context.BIND_AUTO_CREATE);
                break;
            case R.id.unbind_service:
                unbindService(this);
                mainService = null;
                break;
            case R.id.current_number:
                if (mainService != null) {
                    System.out.println("当前服务中的数字：" + mainService.getCurrentNum());
                }
                break;
        }
    }

    @Override
    /**
     * 绑定服务时触发
     */
    public void onServiceConnected(ComponentName name, IBinder service) {
        System.out.println("Bind Service successfully");
        mainService = ((MainService.ServiceBinder) service).getService();//获取服务
    }

    @Override
    /**
     * 一般在服务崩溃时触发，解绑服务不会触发
     */
    public void onServiceDisconnected(ComponentName name) {
        System.out.println("Service Error");
    }
}
